package com.ohos.carbon.slice.dialog;


import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.window.dialog.CommonDialog;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class DialogFragmentActivity extends AbilitySlice {

    public Image back_button, image1, image2;
    public Text text_title;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_activity_dialogfragment);
        initView();

        Button button = (Button) findComponentById(ResourceTable.Id_button5);
        button.setClickedListener(component -> {
            component = LayoutScatter.getInstance(getContext())
                    .parse(ResourceTable.Layout_activity_about, null, true);
            CommonDialog commonDialog = new CommonDialog(getContext());
            commonDialog.setContentCustomComponent(component);
            commonDialog.setSize(1000, 2000);
            Image backButton = (Image) component.findComponentById(ResourceTable.Id_back_button);
            image1 = (Image) component.findComponentById(ResourceTable.Id_image1);
            image2 = (Image) component.findComponentById(ResourceTable.Id_image2);
            image1.setVisibility(Component.VISIBLE);
            image2.setVisibility(Component.VISIBLE);
            text_title = (Text) component.findComponentById(ResourceTable.Id_title_text);
            text_title.setText("");
            backButton.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_clear));
            backButton.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    commonDialog.hide();
                }
            });
            commonDialog.show();
        });

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Dialog fragment")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Dialog fragment")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Dialog fragment", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Dialog fragment", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Dialog fragment", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        image1 = (Image) findComponentById(ResourceTable.Id_image1);
        image2 = (Image) findComponentById(ResourceTable.Id_image2);
        image1.setVisibility(Component.VISIBLE);
        image2.setVisibility(Component.VISIBLE);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Dialog fragment");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        back_button.setClickedListener(component -> {
            onBackPressed();
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

}
